﻿package nid.ui.controls
{
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
    import flash.display.SimpleButton;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;	
	import caurina.transitions.Tweener;
	import nid.events.CalendarEvent;
	import nid.ui.controls.datePicker.CalendarSkin;
	import nid.ui.controls.datePicker.DateField;
	import nid.ui.controls.datePicker.iconSprite;
	/**
	 * ...
	 * @author Nidin Vinayak
	 */
	public class DatePicker extends CalendarSkin {		
		
		
		
		public function get getSelectedDate():Date { return selectedDate; }		
		
		public final function DatePicker() {
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		private function init(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			Construct();
			dateField 		= new DateField();
			addChild(dateField);
			calendarIcon	= new iconSprite();
			calendarIcon.addEventListener(CalendarEvent.LOADED, update);
			calendarIcon.configIcon(_icon);
			addChild(calendarIcon);
		}
		protected function update(e:CalendarEvent):void {
			calendarIcon.x 	= dateField.width + 5;
			isHidden		= true;
			Calendar.x		= calendarIcon.x + calendarIcon.width + 5;
			addCustomMenuItems();
			calendarIcon.addEventListener(MouseEvent.CLICK,showHideCalendar);
			addEventListener(MouseEvent.MOUSE_OVER,MouseOver);
			addEventListener(MouseEvent.MOUSE_OUT,MouseOut);
			addEventListener(MouseEvent.CLICK,MouseClick);
		}
		/*
		 *	CONTEXT MENU 
		 * 
		 */
        private function addCustomMenuItems():void {
			
			myMenu = new ContextMenu();
            myMenu.hideBuiltInItems();
            var menu1:ContextMenuItem;
			var menu2:ContextMenuItem;
            menu1 = null;
			menu1 = new ContextMenuItem("An iGi Lab Production");
            menu2 = new ContextMenuItem("Follow us");			
            menu1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, navigateToSite);
			menu2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, navigateToSite);
            myMenu.customItems.push(menu1);
			myMenu.customItems.push(menu2);
            this.contextMenu = myMenu;
            return;
        }	
        private function navigateToSite(e:ContextMenuEvent):void
        {
           	navigateToURL(new URLRequest("http://www.infogroupindia.com/blog"), "_blank");
            return;
        }				
//=======================================================================================================
//							MOUSE EVENT LISTENERS
//=======================================================================================================		
		public function showHideCalendar(e:MouseEvent):void {
			if (e.currentTarget == stage) {
				trace(e.target.name);
				if (e.target.name == "hit" || 
					e.target.name == "TodayButton" || 
					e.target.name == "YearChanger" || 
					e.target.name == "NextButton" || 
					e.target.name == "PrevButton" || 
					e.target == calendarIcon ||
					String(e.target.name).indexOf("Button") != -1){					
					trace(e.currentTarget);				
					return;
				}
			}
			if (isHidden) {
				addChild(Calendar);
				Tweener.addTween(Calendar, { alpha:1, time:1, transition:"easeOutExpo" } );
				isHidden	=	false;
				try{
					stage.addEventListener(MouseEvent.MOUSE_UP, showHideCalendar);
				}catch (e:Error) {}
			}else {
				Tweener.addTween(Calendar, { alpha:0, time:0.5, transition:"easeOutExpo",onComplete:function ():void{ removeChild(Calendar); } } );
				isHidden	=	true;
				try{
					stage.removeEventListener(MouseEvent.MOUSE_UP, showHideCalendar);
				}catch (e:Error) {}				
			}
		}
		public function MouseOver(e:MouseEvent):void {
			if(!isHidden){
			if(e.target.name == "hit"){
				if(!e.target.parent.hitted)
				changeColor(e.target.parent,mouseOverCellColor);
			}else{
				return;
			}
			}
		}
		public function MouseOut(e:MouseEvent):void {
			if(!isHidden){
			if(e.target.name == "hit"){
				if(!e.target.parent.hitted)
				changeColor(e.target.parent,e.target.parent.id);
			}else{
				return;
			}
			}
		}
		public function MouseClick(e:MouseEvent):void {
			if (!isHidden) {
				switch(e.target.name)
				{
					case "hit":
					{
						e.target.parent.hitted		=	true;
						isHitted.status 			=	true;
						isHitted.num				=	e.target.parent.serial;
						if(oldHit != undefined){
							cellArray[oldHit].hitted 	= 	false;
							changeColor(cellArray[oldHit],cellArray[oldHit].id);
						}
						oldHit			=	e.target.parent.serial;
						//selectedDate	=	new Date(e.target.parent.date.getDate()+ "/" + (currentmonth + 1) + "/" + currentyear;
						var d:Date 		= 	new Date();
						selectedDate	=	new Date(currentyear, currentmonth + 1, e.target.parent.date.getDate(), d.hours, d.minutes, d.seconds, d.milliseconds);;
						dateField.text	=	e.target.parent.date.getDate()+ "/" + (currentmonth + 1) + "/" + currentyear;
						dispatchEvent(new CalendarEvent(CalendarEvent.CHANGE, selectedDate));
						showHideCalendar(e);
						if(!e.target.parent.isToday){ changeColor(e.target.parent,mouseOverCellColor); }
						
						break;
					}
					case "TodayButton":
					{
						trace('today clicked');
						
						var tmpdate:Date = new Date();
						trace(currentyear,tmpdate.fullYear - currentyear);
						currentmonth = tmpdate.month;
						changeYear(tmpdate.fullYear - currentyear);
						ConstructCalendar();
						selectedDate	=	new Date();
						dateField.text	=	selectedDate.getDate()+ "/" + (currentmonth + 1) + "/" + currentyear;
						dispatchEvent(new CalendarEvent(CalendarEvent.CHANGE, selectedDate));
						break;
					}					
					case "YearChanger":
					{
						yearChanger.x = 0;
						yearChanger.y = 0;
						yearChanger.width = calendarWidth;
						yearChanger.height = calendarHeight;
						Calendar.setChildIndex(yearChanger, Calendar.numChildren - 1);
						yearChanger.popup();
						trace('YearChanger clicked');
						break;
					}
					case "Button":
					{
						
						
						break;
					}
					default:
					{
						return;
					}
				}
			}
		}
	}
}